<template>
  <el-dialog
    :title="title"
    v-model="model.dialogFormVisible"
    destroy-on-close
    width="45vw"
    @close="handleCancel"
  >
    <el-form
      :model="model.formData"
      ref="formRef"
      @submit.prevent
      :rules="model.formRules"
      label-width="100px"
    >
      <el-row>
        <el-col>
          <el-form-item label="姓名" prop="userInfo">
            <el-select
              v-model="model.formData.userInfo"
              style="width: 100%"
              filterable
              clearable
              remote
              placeholder="请输入姓名搜索"
              :remote-method="getTableData"
              value-key="id"
              @change="changeUser"
            >
              <el-option
                id="default-option"
                v-for="item in userList"
                :key="item.id"
                :label="item.name"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row justify="end">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </el-row>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import { getUserSysList } from "/@/api/user";
export default defineComponent({
  props: {
    title: {
      type: String,
      default:""
    }
  },
  emits: ["sure", "cancel"],
  setup(props, ctx) {
    const formRef = ref<HTMLFormElement | null>(null) as any;
    const userList = ref([]);
    const model = reactive({
      dialogFormVisible: true,
      formData: {
        userInfo: null as any,
      },
      formRules: {
        userInfo: [{ required: true, message: "请选择成员", trigger: "change" }],
      },
    });

    const changeUser = item => {
      model.formData.userInfo = item
    };

    const handleSubmit = async () => {
      const isValid = await new Promise(resolve =>
        formRef.value.validate(resolve)
      );
      if(isValid){
        ctx.emit("sure",model.formData);
      }
    };

    const handleCancel = async () => {
      ctx.emit("cancel");
    };

    const getTableData = async (query = "") => {
      if (!query) {
        userList.value = [];
        return;
      }
      try {
        const params = {
          pageNum: 1,
          pageSize: 200,
          nameLike: query
        };
        const { code, data } = await getUserSysList(params);
        if (code === 200) {
          userList.value = data.list.map(item => {
            return {
              id: item.id,
              name: item.name + (item.companyName ? "-" + item.companyName : "-无"),
            };
          });
        }
      } catch (err) {
        console.log(err);
      }
    }

    return {
      changeUser,
      userList,
      model,
      formRef,
      handleCancel,
      handleSubmit,
      getTableData
    };
  }
});
</script>
<style lang="scss" scoped>
.btnClass {
  margin-bottom: 10px;
}
</style>
